package cm.hyb.Controller;

import java.util.Map;

//import javax.servlet.http.Cookie;
//import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cm.hyb.Controller.model.User;

//访问路径：项目名/模块路径/方法路径

@Controller
@RequestMapping("/hello")
public class HelloController {
	
	
//	@RequestMapping("info")  //访问这个方法的路径  /hello/info  支持通配符的使用：value="/*/info"
//	@RequestMapping(value="/{addr}/info",method=RequestMethod.GET/*设置请求方法，post不支持*/,params= {"name","age!=10"}/*带参数访问*/,headers= {"user-Agent"}/*请求头必须带有user-Agent*/)  
	@GetMapping(value="/{addr}/info",headers= {"User-Agent"})
	@ResponseBody
	public String info(@PathVariable("addr") String a,@RequestHeader("User-Agent") String ua) {
		
		return "<h1>这里是一个独立的Controller文件，请求头值："+ua+"路径变量"+a+"</h1>";
	}/*
//	方法一
	@GetMapping("testcook")
	public @ResponseBody String cook(HttpServletRequest request) {
		Cookie[] cs = request.getCookies();
		String cv = "";
		for (Cookie cookie : cs) {
			if(cookie.getName().equals("Webstorm-2962ecbd=b31d4b53-92dc-4bcc-a084-920d983277cf")) {
				cv=cookie.getValue();
			}
		}
		return cv;
	}
//	方法二
	@GetMapping("testcook2")
	public @ResponseBody String cook2 (@CookieValue("Webstorm-2962ecbd=b31d4b53-92dc-4bcc-a084-920d983277cf") String sv) {
		return sv;
		
	}*/
	
	/*@GetMapping("login")
	public @ResponseBody Object login(@Valid User user) {
		user.setUserName("刘德华");
		return user;
	}*/
	
	@GetMapping("login")
	public @ResponseBody Object login(User user,Map<String, Object> map) {
		user.setUserName("刘德华");
		map.put("user", user);
//		return "users";
		return user;
	}
}
